<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>添加用户</title>
        <link rel="icon" th:href="@{/static/favicon.ico}" />
        <link rel="stylesheet" th:href="@{/static/bootstrap/css/bootstrap.min.css}" />
        <link rel="stylesheet" th:href="@{/static/admin/css/main.css}" />
        <link rel="stylesheet" th:href="@{/static/admin/css/user_index.css}" />
        <script th:src="@{/static/js/vue.min.js}"></script>
        <script th:src="@{/static/js/axios.min.js}"></script>
        <script th:src="@{/static/js/jquery-3.3.1.min.js}"></script>
        <script th:src="@{/static/bootstrap/js/bootstrap.bundle.js}"></script>
        <script th:src="@{/static/admin/js/config.js}"></script>
        <script th:src="@{/static/admin/js/user_script.js}"></script>
    </head>

    <body>
        <main>
            <div class="main">
                <div class="title-box">
                    <h5>新增用户</h5>
                </div>
                <div class="form-box">
                    <div class="row">
                        <div class="col-5">
                            <form>
                                <div class="form-group">
                                    <label>用户名</label>
                                    <input type="text" id="username" v-model="username" class="form-control" />
                                    <small class="form-text text-muted"></small>
                                </div>
                                <div class="form-group">
                                    <label>密码</label>
                                    <input type="password" id="password" v-model="password" class="form-control" />
                                    <small class="form-text text-muted"></small>
                                </div>
                                <div class="form-group">
                                    <label>真实姓名</label>
                                    <input type="text" id="realName" v-model="realName" class="form-control" />
                                    <small class="form-text text-muted"></small>
                                </div>
                                <div class="form-group">
                                    <label>性别</label><br />
                                    <input type="radio" id="male" name="gender" value="男" v-model="gender" />
                                    <label style="margin-bottom: 0; margin-right: 25px" for="male">男</label>
                                    <input type="radio" id="female" name="gender" value="女" v-model="gender" />
                                    <label style="margin-bottom: 0; margin-right: 25px" for="female">女</label>
                                </div>
                                <div class="form-group">
                                    <label>年龄</label>
                                    <input type="number" id="age" v-model="age" min="18" max="60" class="form-control" />
                                    <small class="form-text text-muted"></small>
                                </div>
                                <div class="form-group">
                                    <label>手机</label>
                                    <input type="tel" id="phone" v-model="phone" class="form-control" />
                                    <small class="form-text text-muted"></small>
                                </div>
                                <div class="form-group">
                                    <label>邮箱</label>
                                    <input type="email" id="email" v-model="email" class="form-control" />
                                    <small class="form-text text-muted"></small>
                                </div>
                                <div class="form-group">
                                    <label>权限</label><br />
                                    <input type="radio" id="guest" name="role" value="3" v-model="role" />
                                    <label style="margin-bottom: 0; margin-right: 25px" for="guest">顾客</label>
                                    <input type="radio" id="manager" name="role" value="2" v-model="role" />
                                    <label style="margin-bottom: 0; margin-right: 25px" for="manager">经营者</label>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>

                <div class="submit-box" style="border-top: 1px solid #ccc">
                    <button type="submit" class="btn btn-primary" @click="JustDoIt">确定提交</button>
                    <button type="button" onclick="window.location.reload()" class="btn btn-outline-secondary">刷新</button>
                </div>
            </div>
        </main>
        <script>
            var vue = new Vue({
                el: ".main",
                data: {
                    username: "",
                    password: "",
                    realName: "",
                    gender: "M",
                    age: "",
                    phone: "",
                    email: "",
                    role: "3",
                },
                methods: {
                    JustDoIt() {
                        axios
                            .post("/user/crud", {
                                username: vue.username,
                                password: vue.password,
                                realName: vue.realName,
                                gender: vue.gender,
                                age: vue.age,
                                phone: vue.phone,
                                email: vue.email,
                                rid: vue.role,
                            })
                            .then((response) => {
                                if (response.data.startsWith("success")) {
                                    alert("添加成功！");
                                    window.location.reload();
                                } else {
                                    alert(response.data);
                                }
                            })
                            .catch((error) => {
                                alert("请求出错：" + error);
                                window.location.reload();
                            });
                    },
                },
            });
        </script>
    </body>
</html>
